const { build } = require('vite');
const { resolve } = require('path');
const { writeFileSync } = require('fs');

async function buildCore() {
    try {
        // 构建 packages/core
        await build({
            build: {
                lib: {
                    entry: resolve(__dirname, '../packages/core/index.ts'),
                    name: 'VueBusinessHooks',
                    fileName: (format) => `vue-business-hooks.${format}.js`,
                    formats: ['es']
                },
                rollupOptions: {
                    external: ['vue'],
                    output: {
                        globals: {
                            vue: 'Vue'
                        }
                    }
                },
                outDir: resolve(__dirname, '../packages/core/dist'),
                emptyOutDir: true,
                sourcemap: true,
                minify: true
            }
        });

        // 生成 package.json
        const pkg = {
            name: '@vue-business-hooks/core',
            version: '1.0.0',
            type: 'module',
            main: './dist/vue-business-hooks.es.js',
            module: './dist/vue-business-hooks.es.js',
            types: './dist/index.d.ts',
            exports: {
                '.': {
                    types: './dist/index.d.ts',
                    import: './dist/vue-business-hooks.es.js'
                }
            },
            files: [
                'dist'
            ],
            peerDependencies: {
                vue: '^3.0.0'
            }
        };

        writeFileSync(
            resolve(__dirname, '../packages/core/package.json'),
            JSON.stringify(pkg, null, 2)
        );

        console.log('Build completed successfully!');
    } catch (error) {
        console.error('Build failed:', error);
        process.exit(1);
    }
}

buildCore(); 